package cn.com.greatwall.common.utils;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

/**
 * @Author herw
 * @Time 2022-06-13 10:35:51
 * @Version 1.0
 * @Description: spring获取bean工具类
 */
@Component
@Order(0)
public class SpringUtil implements ApplicationContextAware {
    private static ApplicationContext applicationContext = null;
    
    public static <T> T getBean(Class<T> cls) {
        return applicationContext.getBean(cls);
    }
    
    public static <T> T getBean(String name, Class<T> cls) {
        return applicationContext.getBean(name, cls);
    }
    
    public static String getProperty(String key) {
        return applicationContext.getBean(Environment.class).getProperty(key);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        SpringUtil.applicationContext = applicationContext;
    }

}
